package com.agenda.server.recebedores;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import com.agenda.comum.ObjectRecebedorInformacao;
import com.agenda.model.AnexoCompromisso;
import com.agenda.util.FileUtil;

public class FazerDownloadAnexoRecebedor extends ObjectRecebedorInformacao {

	private AnexoCompromisso anexo;

	@Override
	public void receber(ObjectInputStream objectInput) throws Exception {
		anexo = (AnexoCompromisso) objectInput.readObject();

	}

	@Override
	protected void responder(OutputStream outputStream) throws Exception {

		BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(new File(anexo.getCaminho())));
		BufferedOutputStream bufferOutput = new BufferedOutputStream(outputStream, 1024);

		FileUtil.enviar(bufferOutput, fileInputStream, sck.getInputStream());

		fileInputStream.close();

		int confirmou = sck.getInputStream().read();

		if (confirmou == 1)
			System.out.println("Confirmou recebimento do arquivo.");

	}

}
